package cn.itrip.auth.controller;

import cn.itrip.auth.service.LoginService;
import cn.itrip.beans.domain.ItripUser;
import cn.itrip.beans.dtos.Dto;
import cn.itrip.common.DtoUtils;
import cn.itrip.common.MD5;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
@RestController
@RequestMapping("/api")
public class LoginController {
    @Resource
    private LoginService loginService;
    @RequestMapping(value = "/dologin",method = RequestMethod.POST)
    public Dto login(String name, String password, HttpServletRequest request){
        String userAgent = request.getHeader("User-Agent");
        ItripUser itripUser = loginService.Login(name.trim(), MD5.md5(password).trim());
        if(itripUser !=null){
            if(itripUser.getActivated() == 0){
                return DtoUtils.returnFail("该账号还没有激活，请先激活","30002");
            }else{
                return DtoUtils.returnSuccess("登录成功",loginService.createToken(itripUser,userAgent));
            }
        }else{
            return DtoUtils.returnFail("用户名或密码错误","30002");
        }

    }
}
